package com.sotnyk.sspeed.utils;

import java.util.Locale;

/**
 * Utility class for conversion between GPS m/s and speed units selected by user
 */
public class SpeedUnitsConverter {
    private final SsAppState mState;

    public SpeedUnitsConverter(SsAppState state) {
        mState = state;
    }

    public float speedInCurrentUnits(float speed) {
        float result = 0;
        SpeedUnits currentSpeedUnits = mState.getCurrentSpeedUnits();
        switch (currentSpeedUnits) {
            case KILOMETERS_PER_HOUR:
                result = speed * 3.6f;
                break;
            case MILES_PER_HOUR:
                result = speed * 2.23694f;
                break;
            case METERS_PER_SECOND:
                result = speed;
                break;
        }
        return result;
    }

    public String speedToText(float speed, String[] speedUnitsSuffixes) {
        String result;
        float convertedSpeed = speedInCurrentUnits(speed);
        SpeedUnits currentSpeedUnits = mState.getCurrentSpeedUnits();
        result = String.format(Locale.getDefault(), "%.1f", convertedSpeed);
        result += "\n" + speedUnitsSuffixes[currentSpeedUnits.ordinal()];
        return result;
    }
}
